package tz.postprocessing;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.Iterator;

import tz.data.Constantes;

public class ResultsAnalyser {

	
	public static TreeMap<String, HashSet<Integer>> clustersTopics;
	public static TreeMap<String, HashSet<Integer>> clustersTopicsResults;
	public static LinkedList<AnalysedResult> listOfResults = new LinkedList<AnalysedResult>();
	public static int stDoc = Constantes.NUMOFARTICLES;
	public static int stCategories = Constantes.NUMOFTOPICS;
	public static String fileNameTest;
	
	public static void ReadTestData(String filenameTest){
		DataParser dp = new DataParser();
		
		clustersTopics = dp.readFromFile(filenameTest);
		
	}
	
	public static void ResultAnalyserData( String filenameResults){
		listOfResults = new LinkedList<AnalysedResult>();


		//String filenameTest = "data/TestTopics/200TestTopics_8";
		DataParser dp = new DataParser();
	
	
		
		clustersTopicsResults = dp.readFromFile(filenameResults);
		
		//po vseh topicih c testnih podatkih
		for(String topic: clustersTopics.keySet()){
			HashSet<Integer> ArticleIdTest = clustersTopics.get(topic);
			AnalysedResult anRes = new AnalysedResult(topic, ArticleIdTest.size());

			
			//po vseh topicih v rezultatih
			for(String topicRes: clustersTopicsResults.keySet()){
				HashSet<Integer> ArticleIdResult = clustersTopicsResults.get(topicRes);
				Iterator<Integer> iter = ArticleIdResult.iterator();
				
				//Stevec, koliko je enakih clankov v skupnih clustrih
				int counterResult = 0;
				//za vsak articleID v testnih podatkih nekega topica, pogledamo ce je vsebovan v  topicu rezultata.
				while(iter.hasNext()){
					Integer id = iter.next();
					if (ArticleIdTest.contains(id)){			
						counterResult++;
					}
				}
				anRes.addClusterResult(topicRes, counterResult, ArticleIdResult.size());

				
			}
			listOfResults.add(anRes.clone());
		}
		
		//System.out.println();
		//WriteListOfResult();
		
		System.out.print("\t" +calcPurity());
		System.out.print("\t" +calcEntropy());
		System.out.println();
	}
	
	public static void WriteListOfResult(){
		for(AnalysedResult res : listOfResults){
			//String print = res.getClustersAndMatches();
			String print = res.getClusterMatchesAndMostMathes();
			System.out.println(print);
		}
		
	}

	
	public static double calcPurity(){
		double purity = 0;
		for(AnalysedResult res : listOfResults){
			
			double puirityOne = (1 /  (double)res.numOfCasesInCluster)*(double)res.clusterResultnum;
			purity = purity + (((double)res.numOfCasesInCluster /  (double)Constantes.NUMOFARTICLES) * puirityOne);		
			if(puirityOne>1){
			//System.out.println(purity+"  "+res.clusterTest+" "+res.numOfCasesInCluster+" "+res.clusterResultMost+" "+res.clusterResultnum );
			}
		}
		return purity;
	}
	
	public static double calcEntropy(){
		
		double entropy = 0;
		for(AnalysedResult res : listOfResults){
			double entropyOfOne = (1/AnalysedResult.log2x((double)stCategories))*res.entropy;
			entropy = entropy + (((float)res.numOfCasesInCluster / (float)Constantes.NUMOFARTICLES) * entropyOfOne); 
		}
		return entropy;
		
	}
}
